<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${d_u}">

	<!-- 列定义 -->
	<sql id="columns">
	   <#list colList as col>  
	       <#if col_has_next>
	          ${col.db_column},
	      <#else>
	      	  ${col.db_column}
	       </#if>   
       </#list>
	</sql>
	
	
	<!-- 插入-->
	<insert id="save" parameterType="${m_u}" useGeneratedKeys="true" keyProperty="id">
		INSERT INTO ${tbName}(
		<#list colList as col>
		<#if col.isPK!=1>
	        <#if col_has_next>
	          ${col.db_column},
	        <#else>
	          ${col.db_column})
			</#if>
		</#if>
		</#list>
		VALUES(
		<#list colList as col>
		<#if col.isPK!=1>
	        <#if col_has_next>
	          <#if 1==1>#</#if>{${col.columnName},jdbcType=${col.db_fieldName}},
	        <#else>
	          <#if 1==1>#</#if>{${col.columnName},jdbcType=${col.db_fieldName}})
	        </#if>
		</#if>
		</#list>
	</insert>
	
	
	<!--删除定义-->
	<#list colList as col>
	<#if col.isPK==1>
	<delete id="deleteById" parameterType="${col.fieldType}">
		DELETE FROM ${tbName} WHERE ${col.db_column} = <#if 1==1>#</#if>{${col.columnName},jdbcType=${col.db_fieldName}}   
	</delete>
	</#if>
	</#list>

	<!-- 更新通过主键-->
	<update id="updateById" parameterType="${m_u}">
		UPDATE ${tbName}
		<set>
			<#list colList as col>
			<#if col.isPK!=1>
			<#if col_has_next>
			<if test="${col.columnName}!=null">
			${col.db_column}=<#if 1==1>#</#if>{${col.columnName},jdbcType=${col.db_fieldName}},   
			</if>
			<#else>
			<if test="${col.columnName}!=null">
			${col.db_column}=<#if 1==1>#</#if>{${col.columnName},jdbcType=${col.db_fieldName}},   
			</if>
			</#if> 
			</#if>
			</#list>  
			</set>
			WHERE 
			<#list colList as col>
		    <#if col.isPK==1>
		    ${col.db_column} =<#if 1==1>#</#if>{${col.columnName},jdbcType=${col.db_fieldName}}
		    </#if>
			</#list>
	</update>
		
	<#list colList as col>
	<#if col.isPK==1>
	<!-- 查询通过主键-->
	<select id="selectById" resultMap="resultMap" parameterType="${col.fieldType}">
		SELECT <include refid="columns" /> FROM ${tbName} WHERE ${col.db_column} = <#if 1==1>#</#if>{${col.columnName},jdbcType=${col.db_fieldName}}   
	</select>
	</#if>
	</#list>
	
	<!-- 查找所有-->
	<select id="query${className}All" resultMap="resultMap">
		SELECT <include refid="columns" /> FROM ${tbName}
	</select>

    <sql id="whereSql">
        <where>
			<#list colList as col>
			<if test="param.${col.columnName}!=null">
				AND ${col.db_column} = <#if 1==1>#</#if>{param.${col.columnName},jdbcType=${col.db_fieldName}}  
			</if>
			</#list>
			 <if test="page!=null AND page.startTime!=null">
                AND create_time>=<#if 1==1>#</#if>{page.startTime,jdbcType=TIMESTAMP}
            </if>
            <if test="page!=null AND page.endTime!=null">
                AND create_time &lt;=<#if 1==1>#</#if>{page.endTime,jdbcType=TIMESTAMP}
            </if>
		</where>	 
    </sql>

    <select id="countByPage" parameterType="${m_u}" resultType="int">
        SELECT COUNT(id) FROM ${tbName} <include refid="whereSql"/>
    </select>

    <!-- 分页查询 -->
    <select id="queryByPage" parameterType="${m_u}" resultMap="resultMap">
        SELECT <include refid="columns"/> FROM ${tbName} <include refid="whereSql"/>
        <choose>
            <when test="page.sortName!=null">
                ORDER BY <#if 1==1>$</#if>{page.sortName} <#if 1==1>$</#if>{page.sortOrder}
            </when>
            <otherwise>
                ORDER BY update_time <#if 1==1>$</#if>{page.sortOrder}
            </otherwise>
        </choose>
        LIMIT <#if 1==1>#</#if>{page.start,jdbcType=INTEGER}, <#if 1==1>#</#if>{page.pageSize,jdbcType=INTEGER}
    </select>

    <#list colList as col>
		<#if col_has_next>
	       <#if col.db_column == "is_deleted">
	<update id="modifyDeleteStatus">
	UPDATE ${tbName} SET ${col.db_column}= <#if 1==1>#</#if>{isDeleted},update_time=<#if 1==1>#</#if>{curDate} WHERE id=<#if 1==1>#</#if>{id}
	</update>
	       </#if>
		</#if>
	</#list>

	<!--通过ID集合查询-->
	<select id="queryByIdList" resultMap="resultMap">
        SELECT  <include refid="columns"/> FROM ${tbName}
        <where>
            <if test="itemList!=null">
                 id IN
                <foreach collection="itemList" item="item" open="(" close=")"  separator=",">
                    <#if 1==1>#</#if>{item}
                </foreach>
            </if>
        </where>
    </select>
	
	<!-- 通过索引查询-->
	<#list idxList as idx>
	<select id="query${className}By${idx.idxName}" resultMap="resultMap">
		SELECT <include refid="columns"/> FROM ${tbName}
		<where>
			<#list idx.colList as idxCol>
			<if test="${idxCol.columnName}!=null">
				AND ${idxCol.db_column} = <#if 1==1>#</#if>{${idxCol.columnName},jdbcType=${idxCol.db_fieldName}} 
			</if>
			</#list>
		</where>
	</select>
	</#list>

	<!-- 数据库列与对象属性映射 -->
    <resultMap type="${m_u}" id="resultMap">
         <#list colList as col>
            <#if col.isPK==1>
                <id property="${col.columnName}" column="${col.db_column}" javaType="${col.fieldType}"  jdbcType="${col.db_fieldName}"/>
 			<#else>
                <result property="${col.columnName}" column="${col.db_column}" javaType="${col.fieldType}"  jdbcType="${col.db_fieldName}"/>
            </#if>       
         </#list>
     </resultMap>
</mapper>